package com.niit.onlinemusicserver.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.niit.onlinemusicserver.constant.Constants;
import com.niit.onlinemusicserver.mapper.ConsumerMapper;
import com.niit.onlinemusicserver.common.R;
import com.niit.onlinemusicserver.model.domain.Consumer;
import com.niit.onlinemusicserver.service.ConsumerService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.Date;

@Service
public class ConsumerServiceImpl extends ServiceImpl<ConsumerMapper, Consumer>
        implements ConsumerService {
  @Autowired
  private ConsumerMapper consumerMapper;
  @Override
  public R allUser(IPage page) {
    return R.success("查询成功",consumerMapper.selectPage(page,null));
  }

  /**
   * 删除用户
   */
  @Override
  public R deleteUser(Integer id) {
    if (consumerMapper.deleteById(id) > 0) {
      return R.success("删除成功");
    } else {
      return R.error("删除失败");
    }
  }


  /**
   * 删除多个用户
   */
  @Override
  public R deleteUsers(String[] ids) {
    //把数组中参数组织成SQL语句需要的格式
    StringBuffer sb = new StringBuffer();
    for (String i:ids) {
      sb.append(i+",");
    }
    sb.deleteCharAt(sb.length()-1);
    if (consumerMapper.deleteByIds(sb.toString()) > 0) {
      return R.success("删除成功");
    } else {
      return R.error("删除失败");
    }
  }

  @Override
  public R userOfId(Integer id) {
    QueryWrapper<Consumer> queryWrapper = new QueryWrapper<>();
    queryWrapper.eq("id",id);
    return R.success(null, consumerMapper.selectList(queryWrapper));
  }

  @Override
  public R allUsers() {
    return R.success("查询成功",consumerMapper.selectList(null));
  }










}